package top.hekun.study.runtime;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Arrays;

/**
 * @Author: 贺坤
 * @Date: 2023/5/24 10:00
 */
public class RuntimeMain {
    public static void main(String[] args) throws IOException, InterruptedException, NoSuchFieldException, IllegalAccessException {
        ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/k","start","run8081.bat");
//        ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/k","start" ,"ping" ,"192.168.20.202");
        File filePath= new File(new File(new File(new File(System.getProperty("user.dir"),"test"),"test-web"),"build"),"libs");
        System.out.println(Arrays.toString(filePath.list()));
        Process process = processBuilder
//                .redirectErrorStream(true)
                .directory(filePath)
                .start();
        Thread.sleep(2000L);
        System.out.println(process.isAlive());
        Class<? extends Process> processClass = process.getClass();
        Field declaredField = processClass.getDeclaredField("pid");
        declaredField.setAccessible(true);
        long pid = declaredField.getLong(process);
        declaredField.setAccessible(false);

        System.out.println("pid  " +pid);
//        System.out.println(process);
////        System.out.println(process.waitFor());
////        System.out.println(process.destroyForcibly());
//        Thread.sleep(2000L);
//        System.out.println(process.isAlive());
//        Thread.sleep(2000L);
//        System.out.println(process.isAlive());
//        {process.waitFor();}
        System.out.println("****************");

    }
}
